﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Vanfa.Queries.Sql.DbModel;

namespace Vanfa.Queries.Sql.DbQueries
{
    public class AlbumQry : TDbImpl<AlbumQry>
    {
        public int Insert(picUserPhoto entity)
        {
            var cmd = VanfaDb.GetStoredProcCommand("up_Album_UploadPhoto");
            VanfaDb.AddInParameter(cmd, "UserID", DbType.Int32, entity.UserID);
            VanfaDb.AddInParameter(cmd, "PhName", DbType.AnsiString, entity.PhName);
            VanfaDb.AddInParameter(cmd, "PhotoPath", DbType.AnsiString, entity.PhotoPath);
            VanfaDb.AddInParameter(cmd, "ThumbPath", DbType.AnsiString, entity.ThumbPath);
            VanfaDb.AddInParameter(cmd, "ViewCount", DbType.Int32, entity.ViewCount);
            VanfaDb.AddInParameter(cmd, "PhDesc", DbType.AnsiString, entity.PhDesc);
            entity.PhotoID = Convert.ToInt32(VanfaDb.ExecuteScalar(cmd));
            return entity.PhotoID;
        }

        public IList<picUserPhoto> Select(int userID)
        {
            var cmd = VanfaDb.GetStoredProcCommand("up_Album_SelectPhotoByUserID");
            VanfaDb.AddInParameter(cmd, "UserID", DbType.Int32, userID);
            using (var read = VanfaDb.ExecuteReader(cmd))
            {
                IList<picUserPhoto> ps = new List<picUserPhoto>();
                while (read.Read())
                {
                    ps.Add(this.ConvertTo<picUserPhoto>(read));
                }
                return ps;
            }
        }
    }
}
